<?php
use\ampleharvest\code\usermanager\tables;
/**
 * @package AmpleHarvest
 */

require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/config/cConfig.php';
$codeConfig = new \ampleharvest\code\cConfig();

require_once dirname(dirname(__FILE__)) . "/cMailNotification.php";
require_once dirname(dirname(dirname(__FILE__))) . "/User_Manager/Tables/cTABLE_Groups.php";
require_once dirname(dirname(dirname(__FILE__))) . "/Tables/cTABLE_IRT.php";
require_once dirname(dirname(dirname(__FILE__))) . "/Tables/cTABLE_IRT_Comments.php";
require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users.php";
require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users2Groups.php";

/**
 * @brief Class Extends the cMailNotification Class and is used to send notifictions about Queries
 *
 * - Revision History
 *  - 0.1
 * 	 - <b>Date</b>: 13 APR 2011
 * 	 - <b>Author</b>: Randy Blancett
 * 	  - Initial Version
 *
 * @author Randy Blancett
 * @version 0.1
 * @ingroup code classes
 */
class cIRTNotification extends cMailNotification
{
	const C_INT_TYPE_NEW = 0;
	const C_INT_TYPE_UPDATE = 1;
	const C_INT_TYPE_NEW_COMMENT = 2;

	protected $m_int_Type = SELF::C_INT_TYPE_NEW;

	protected $m_str_RequestID = null;

	protected $m_obj_TABLE_Users = null;
	protected $m_obj_TABLE_Users2Groups = null;
	protected $m_obj_TABLE_IRT = null;
	protected $m_obj_TABLE_Orig_IRT = null;
	protected $m_obj_TABLE_IRT_Comments = null;

	public function __construct()
	{
		$this->m_obj_TABLE_Users = new \cTABLE_Users();
		$this->m_obj_TABLE_Users2Groups = new \cTABLE_Users2Groups();
		$this->m_obj_TABLE_IRT = new cTABLE_IRT();
		$this->m_obj_TABLE_Orig_IRT = new cTABLE_IRT();
		$this->m_obj_TABLE_IRT_Comments = new cTABLE_IRT_COMMENTS();
	}

	/**
	 * Sets the original data (used when sending and update Notification.
	 * @param cTABLE_Harvest_Requests $obj_OrigData The original data object
	 */
	public function set_OriginalData(cTABLE_IRT $obj_OrigData)
	{
		$this->m_obj_TABLE_Orig_IRT = $obj_OrigData;
	}

	/**
	 * Sets the type of notification that will be sent
	 * @param integer $int_Type see constants
	 */
	public function set_Type($int_Type)
	{
		$this->m_int_Type = $int_Type;
	}

	/**
	 * Sets the ID of the harvest Request
	 * @param string $str_RequestID ID of the Request the notification is about (GUID)
	 */
	public function set_RequestID($str_RequestID)
	{
		$this->m_str_RequestID = $str_RequestID;
	}

	public function send($str_UserID = null)
	{
		if (!$this->m_obj_TABLE_IRT->load_DataFromID($this->m_str_RequestID))
		{
			return false;
		}
			
		$managers = $this->m_obj_TABLE_Users2Groups->get_MembersAsArray(tables\cTABLE_Groups::C_STR_GROUP_HARVEST_MANAGER);
		foreach ($managers as $str_temp_UserID)
		{
			if ($this->m_obj_TABLE_Users->load_DataFromUserID($str_temp_UserID))
			{
				$this->add_To($this->m_obj_TABLE_Users->get_Email1());
			}
		}
			
		if ($str_UserID)
		{
			$this->m_obj_TABLE_Users->load_DataFromUserID($str_UserID);
			$this->add_CC($this->m_obj_TABLE_Users->get_Email1());
		}

		switch ($this->m_int_Type)
		{
			case self::C_INT_TYPE_NEW:
				$this->set_Subject("New Harvest Request");
				break;
			case self::C_INT_TYPE_UPDATE:
				$this->set_Subject("Harvest Request " . $this->m_str_RequestID . " has been updated");
				$this->m_obj_TABLE_Users->load_DataFromUserName($this->m_obj_TABLE_IRT->get_importer_sid());
				$this->add_CC($this->m_obj_TABLE_Users->get_Email1());
				break;
			case self::C_INT_TYPE_NEW_COMMENT:
				$this->set_Subject("A new comment has been entered for request #" . $this->m_str_RequestID);
				$this->m_obj_TABLE_Users->load_DataFromUserName($this->m_obj_TABLE_IRT->get_user_sid());
				$this->add_CC($this->m_obj_TABLE_Users->get_Email1());
		}
		$int_Errors = ini_get("display_errors");
			
		ini_set('display_errors', 0);
			
			
			
		$bool_Return = parent::send_EMail();
			
		ini_set('display_errors', $int_Errors);

		return $bool_Return;
	}

	public function get_Body()
	{
		$str_Output = "";

		switch ($this->m_int_Type)
		{
			case self::C_INT_TYPE_NEW:
				$str_Output .= "The following Request has been Received :<hr/>\n";
				$str_Output .= $this->output_Record($this->m_obj_TABLE_IRT);
				break;
			case self::C_INT_TYPE_UPDATE:
				$str_Output .= "The following Request has been updated from:<hr/>\n";
				$str_Output .= $this->output_Record($this->m_obj_TABLE_Orig_IRT);
				$str_Output .= "to:<hr/>\n";
				$str_Output .= $this->output_Record($this->m_obj_TABLE_IRT);
				$str_Output .= "With the following comments:<hr/>\n";
				$str_Output .= $this->output_Record_Comments($this->m_obj_TABLE_IRT);
				break;
			case self::C_INT_TYPE_NEW_COMMENT:
				$str_Output .= "The request has been updated with the following comments:<hr/>\n";
				$str_Output .= $this->output_Record_Comments($this->m_obj_TABLE_IRT);
				break;
		}

		return $str_Output;
	}

	private function output_Record(cTABLE_IRT $obj_Data)
	{
		$arr_Data = $obj_Data->output_AsArray();

		$str_Output = "<table><tbody>";

		foreach ($arr_Data as $key => $data)
		{
			$str_Output .= "<tr>";
			switch ($key)
			{
				case $this->m_obj_TABLE_IRT->get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_CommenterID):
					$this->m_obj_TABLE_Users->load_DataFromUserID($data);
					$str_Output .= "<td><b>User Name</b></td><td>" . $this->m_obj_TABLE_Users->get_UserName() . "</td>\n";
					break;
				default:
					$str_Output .= "<td><b>$key</b></td><td> $data</td>\n";
			}
			$str_Output .= "</tr>";
		}
		$str_Output .= "</tbody></table>";
		return $str_Output;
	}

	private function output_Record_Comments()
	{

		$this->m_obj_TABLE_IRT->clear_Query();

		$this->m_obj_TABLE_IRT_Comments->add_ColumnQuery($this->m_obj_TABLE_IRT_Comments->m_int_COL_RequestID, $this->m_str_RequestID);
		$this->m_obj_TABLE_IRT_Comments->add_Sort_By_Name($this->m_obj_TABLE_IRT_Comments->get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_CommentDate), cConnection::c_str_SORT_DESC);
		$this->m_obj_TABLE_IRT_Comments->do_Search();
			
		$arr_TempRS = $this->m_obj_TABLE_IRT_Comments->get_ResultSet();
		$total = (int)$this->m_obj_TABLE_IRT_Comments->do_Count();

		if ($total > 0)
		{
			$str_Output = "<table><tbody>";
			foreach ($arr_TempRS as $comment)
			{
				$this->m_obj_TABLE_IRT_Comments->load_DataFromArray($comment);
				$arr_Data = $this->m_obj_TABLE_IRT_Comments->output_AsArray();

				/*
				foreach ($arr_Data as $key => $data)
				{
					$str_Output .= "<tr>";
					switch ($key)
					{
						case $this->m_obj_TABLE_IRT_Comments->get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_CommenterID):
							$this->m_obj_TABLE_Users->load_DataFromUserID($data);
							$str_Output .= "<td><b>User Name</b></td><td>" . $this->m_obj_TABLE_Users->get_UserName() . "</td>\n";
							break;
						case $this->m_obj_TABLE_IRT_Comments->get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_ID):
							$str_Output .= "<td><b></b></td><td> </td>\n";
							break;
						case $this->m_obj_TABLE_IRT_Comments->get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_RequestID):
							$str_Output .= "<td><b></b></td><td> </td>\n";
							break;
						default:
							$str_Output .= "<td><b>$key</b></td><td> $data</td>\n";
					}
					$str_Output .= "</tr>";
				}
				*/
				$str_Output .= "<tr>";
				$str_Output .= "\"".
						$arr_Data[$this->m_obj_TABLE_IRT_Comments->
						get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_Comment)]."\"";
				$str_Output .= "</tr>";
				$str_Output .= "<tr>";
				$this->m_obj_TABLE_Users->load_DataFromUserID(
						$arr_Data[$this->m_obj_TABLE_IRT_Comments->
						get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_CommenterID)]);
				$str_Output .= "(<b>" . $this->m_obj_TABLE_Users->get_UserName()."</b> ".
								$arr_Data[$this->m_obj_TABLE_IRT_Comments->get_Column_Name($this->m_obj_TABLE_IRT_Comments->m_int_COL_CommentDate)].")";
				$str_Output .= "</tr>";
			}
			$str_Output .= "</tbody></table>";
			return $str_Output;
		}
			
		return "No Comments Entered";
			
	}
}
?>